package com.hpf.tool.redis.utils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
 * 
 * @author hpf
 *
 */
public final class HJedisPool extends JedisPoolConfig {
	private static JedisPool pool;
	
	/**
	 * redis 主机IP
	 */
	private String host;
	/**
	 * redis 密码
	 */
	private String password;
	/**
	 * redis 端口号
	 */
	private int port;
	
	/**
	 * 初始化 redis 连接配置
	 */
	private void inintPool(){
		if(null == pool){
			pool = new JedisPool(this,host,port,1000,password);
		}
	}
	
	/**
	 * 释放 jedis 资源
	 * @param jedis
	 */
	public void returnResource(final Jedis jedis){
		pool.returnResourceObject(jedis);
	}
	/**
	 * 获取 Jedis
	 * @return Jedis
	 */
	public Jedis getJedis(){
		if(pool == null){
			synchronized(HJedisPool.class){
				inintPool();
				return pool.getResource();
			}
		}
		return pool.getResource();
	}

	public void setHost(String host) {
		this.host = host;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setPort(int port) {
		this.port = port;
	}
}
